/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author ANH
 */
public class Ball {
    private int x;
    private int y;
    private int color;
    private int kind;
    private int curFrame;
    private int nMineAround;
    public boolean istouched;
    public boolean ishide;
    public boolean ismine;
    private Image balls;
    private Sprite ball;
    public Ball(int _x,int _y, Image _ball){
        istouched=false;
        x=_x;
        y=_y;
        balls=_ball;
        nMineAround=0;
        loadGraphics();
    }
    public void loadGraphics() {// can thiet
       
          //  ImageLoader loader = ImageLoader.getInstance();

            ball = new Sprite(balls, 25, 25);
            ball.setRefPixelPosition(25, 25);
            ball.setFrame(0);
            curFrame=0;    
    }
     public void render(Graphics g) {
        int anchor = Graphics.TOP|Graphics.LEFT;
        ball.setPosition(x, y);
        if(istouched)    curFrame++;
        if(curFrame>6)
        {
            curFrame=0;
           
        }
        ball.setFrame(curFrame);
      if(!ishide)  ball.paint(g);
     else  g.drawString(String.valueOf(nMineAround),x+10, y+5, anchor);
    }
     private final Rectangle getArea() {
        return new Rectangle(x, y,25 , 25);
    }

    public final boolean isPressed(int x, int y) {
        return getArea().isPointInside(x, y);
    }
    private void CreateBall(){
        
    }
    public boolean OnSelelected(int x,int y){// dành cho chạm( tạm thời)
        if(isPressed(x,y))
        {
            istouched=true;
            return true;
        }
        return false;
    }
    public void OnSelected(){// dành cho bấm phím (tạm thời)
        istouched = true;
    }
    public boolean Open()
    {
         if(ismine)
         {
            // isexplosed = true;
             return true;
        }
         else 
         {
             ishide=true;
             return false;
        }
    }
    public void setNoMineAround(int n){
        nMineAround=n;
        ishide=true;
    }
    public void reset()
    {
        ismine=false;
        ishide=false;
        istouched=false;
        curFrame=0;
        nMineAround=0;
    }
    public int getX(){
        return x;
    }
    public int getY(){
        return y;
    }
}
